package org.xiaov.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.xiaov.entity.po.UserPo;
import org.xiaov.mapper.UserMapper;
import org.xiaov.service.UserService;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 描述：
 * </p>
 *
 * @author xiaovcloud
 * @since 2021/12/18 12:37
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 保存用户 姓名 性别 出生日期 rabbitmq
     * 填充默认金额 ，
     * 邮件通知用户
     *
     * @param userPo 用户
     * @return
     */
    @Override
    public Boolean saveUser(UserPo userPo) {
        //保存用户 name,gender,birthday
        log.info("保存的数据：姓名：{}，性别：{}，出生日期：{}",
                userPo.getName(), userPo.getGender() ? "男" : "女", userPo.getBirthday());
        userPo.setCreatedon(LocalDateTime.now());
        boolean save = this.save(userPo);
        if (save) {
            String id = userPo.getId();
            log.info("插入成功，id：{}", id);
            //发送消息到队列
            rabbitTemplate.convertAndSend("cloudDeadExchange", "cloudDeadRoutingKey", id);
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateUser(UserPo userPo) {
        return this.updateById(userPo);
    }
}
